package spring.core.aop.aspectj;

import org.aopalliance.aop.Advice;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.aop.support.AopUtils;

import spring.core.aop.DefaultTeacherService;
import spring.core.aop.TeacherMethodInterceptor;
import spring.core.aop.TeacherService;

/**
 * @author 1203056
 * @version Id: AspectjAopFactoryDemo.java, v 0.1 2021-09-07 9:42 1203056 Exp $
 */
public class AspectjProxyFactoryDemo {
    public static void main(String[] args) {
        DefaultTeacherService target = new DefaultTeacherService();
        //DefaultAopProxyFactory
        Object jdkProxy = createProxy(target, null, false); //jdk proxy
        System.out.println("======================= proxy.class = " + jdkProxy.getClass());
        Object newProxy = createProxy(jdkProxy, new TeacherMethodInterceptor(), true);

    }

    public static Object createProxy(Object target, Advice advice, boolean optimize) {

        AspectJProxyFactory proxyFactory = new AspectJProxyFactory(target);
        proxyFactory.setProxyTargetClass(false);
        proxyFactory.setOptimize(optimize);
        //proxyTargetClass=false optimize=false jdk
        proxyFactory.addAspect(TeacherAspect.class);
        if (advice != null) {
            proxyFactory.addAdvice(advice);
        }
        final TeacherService proxy = proxyFactory.getProxy();

        System.out.println("jdk dynamic proxy=" + AopUtils.isJdkDynamicProxy(proxy));
        System.out.println("cglib  proxy=" + AopUtils.isCglibProxy(proxy));
        return proxy;
    }
}
